#!/bin/bash
TEST="True"
LOG_FILE="./vkb_default.log"

pid=$(ps -ef | grep virtkeyboard | grep -v grep | awk '{print $2}')
if [ -n "$pid" ]; then
    kill -9 $pid
else
    echo "start at [$(date)]...." > $LOG_FILE
fi

# 解决依赖缺失问题，在联网环境下自动安装
dpkg -l python3-virtkey > /dev/null
if [ $? != 0 ] ; then
	echo 'No package python3-virkey found, apt-get it.'
	sudo apt-get update
	sudo apt-get install python3-virtkey
fi

if [ ${TEST}_ == "True"_ ]; then  # test for vbox debug
    PATH_APPLICATION="/media/sf_share/en/proj/virtkeyboard"
    MAIN_FILE="vkb.py"
    key_size="0,0,300,700"
else
    PATH_APPLICATION="$NUTHOME/bin/virtkeyboard"
    MAIN_FILE="vkb.pyc"
    key_size=$(samaritan -k)
fi

PATH_CONFIG="./vkb.xml"

# 查到 -1 则退出
test $(expr index ${key_size}_ "-") -gt 0 && exit

# 替换字符
key_size=${key_size/,/ -y }
key_size=${key_size/,/ -w }
key_size=${key_size/,/ -H }

if [ ${TEST}_ == "True"_ ]; then  # test for vbox debug
    python3 ${PATH_APPLICATION}/${MAIN_FILE} $PATH_CONFIG -x $key_size
else
    while true; do
        python3 ${PATH_APPLICATION}/${MAIN_FILE} $PATH_CONFIG -x $key_size >/dev/null 2>&1
        echo "end at [$(date)] !" >> $LOG_FILE
        # sleep 1
    done
fi